package com.epam.edu.dao;

import com.epam.edu.db.ConnectionPool;
import com.epam.edu.entity.TeachingPlan;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class DAOTeachingPlan {
    public static TeachingPlan selectById(Connection connection,int id) {
        TeachingPlan teachingPlan = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            String command = "SELECT id,teachingPlan FROM TeachingPlans WHERE id=?";
            statement = connection.prepareStatement(command);
            statement.setInt(1, id);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                teachingPlan = new TeachingPlan(resultSet.getInt(1), resultSet.getString(2));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
                if (resultSet != null) resultSet.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        return teachingPlan;
    }

    public static List<TeachingPlan> selectAll(Connection connection) {
        List<TeachingPlan> teachingPlans = new LinkedList<TeachingPlan>();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            String command = "SELECT id,teachingPlan FROM TeachingPlans";
            statement = connection.prepareStatement(command);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                TeachingPlan teachingPlan = new TeachingPlan(resultSet.getInt(1), resultSet.getString(2));
                teachingPlans.add(teachingPlan);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
                if (resultSet != null) resultSet.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        return teachingPlans;
    }

    public static TeachingPlan selectByPlanName(Connection connection,String planName) {
        TeachingPlan teachingPlan = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            String command = "SELECT id,teachingPlan FROM TeachingPlans WHERE teachingPlan=?";
            statement = connection.prepareStatement(command);
            statement.setString(1, planName);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                teachingPlan = new TeachingPlan(resultSet.getInt(1), resultSet.getString(2));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
                if (resultSet != null) resultSet.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        return teachingPlan;
    }
}
